<?php

declare(strict_types=1);
/*
 * @Description: 友盟service类
 * @Author: QianLong
 * @Date: 2019-10-13 12:39:05
 * @LastEditors    : QianLong
 * @LastEditTime   : 2023-02-02 17:12:21
 */

namespace App\Service\Push;

use App\Common\RedisCache;
use Yurun\Util\HttpRequest;

class Umeng
{
    protected $serverInfo = [];
    public function __construct($serverInfo)
    {
        $this->serverInfo = $serverInfo;
    }
    /**
     * 单条推送 打开页面
     * 
     */
    public function singlePushStartPage(?array $data = null)
    {
        $return['send_result'] = false;
        if (!empty($data) && is_array($data)) {
            $url = 'http://msg.umeng.com/api/send';
            $post['appkey'] = $this->serverInfo['umeng_app_key'];
            $post['timestamp'] = time();
            if ($data['single_cid'] == 'all') {
                $post['type'] = 'broadcast';
            }else{
                $post['type'] = 'unicast';
                $post['device_tokens'] = $data['single_cid'];
            }
            // if (isset($data['click_type_content']) && !empty($data['click_type_content'])) {
            //     $notice['aps'] = json_decode($data['click_type_content'], true);
            // }
            $notice['display_type'] = 'notification';
            $notice['body']['title'] = $data['title'];
            $notice['aps']['alert']['title'] = $data['title'];
            $notice['aps']['alert']['subtitle'] = $data['title'];
            $notice['body']['text'] = $data['content'];
            $notice['aps']['alert']['body'] = $data['content'];
            $notice['aps']['interruption-level'] = 'active';
            if (isset($this->serverInfo['logo_url']) && !empty($this->serverInfo['logo_url'])) {
                $notice['body']['img'] = $this->serverInfo['logo_url'];
            }
            if (isset($this->serverInfo['umeng_play_vibrate'])) {
                $notice['body']['play_vibrate'] = ($this->serverInfo['umeng_play_vibrate']==1)?'true':'false';
            }
            if (isset($this->serverInfo['umeng_play_lights'])) {
                $notice['body']['play_lights'] = ($this->serverInfo['umeng_play_lights']==1)?'true':'false';
            }
            if (isset($this->serverInfo['umeng_play_sound'])) {
                $notice['body']['play_sound'] = ($this->serverInfo['umeng_play_sound']==1)?'true':'false';
            }
            $click_type = (isset($data['click_type']) && !empty($data['click_type']))?$data['click_type']: 'payload';
            switch ($click_type) {
                case 'startapp':
                    $notice['body']['after_open'] = 'go_app';
                    $notice['body']['activity'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : [];
                    break;
                case 'url':
                    $notice['body']['after_open'] = 'go_url';
                    $notice['body']['url'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : [];
                    break;
                case 'payload':
                    $notice['body']['after_open'] = 'go_custom';
                    $notice['body']['custom'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : [];
                    break;
                default:
                    # code...
                    break;
            }
            $post['payload']= $notice;
            $http = new HttpRequest;
            $sign = $this->getSign($url, $post);
            $response = $http->post($url. '?sign='.$sign, $post, 'json');
            $responseData = json_decode($response->body(), true);
            $return['send_result_data'] = $responseData;
            $return['pushParam'] = $post;
            $return['task_id'] = '';
            if (isset($responseData['ret']) && strtoupper($responseData['ret']) == 'SUCCESS') {
                $return['send_result'] = true;
                $return['send_error'] = '';
                $return['send_error'] = $responseData['data']['msg_id'];
            }else{
                $return['send_error'] = '(' . $responseData['data']['error_code'] . ') ' . $responseData['data']['error_msg'];
            }
            unset($post,$url,$response,$pushData,$http,$data);
            return $return;
        }
    }
    public function singlePush(?array $data = null)
    {
        $return['send_result'] = false;
        if (!empty($data) && is_array($data)) {
            $url = 'https://push.openapi.umeng.com/SendByDevice';
            // $post['appkey'] = $this->serverInfo['umeng_app_key'];
            // $post['timestamp'] = time();
            // $post['type'] = 'unicast';
            $post['DeviceTokens'] = $data['single_cid'];
            // if (isset($data['click_type_content']) && !empty($data['click_type_content'])) {
            //     $notice['aps'] = json_decode($data['click_type_content'], true);
            // }
            $post['AppKey'] = $this->serverInfo['umeng_app_key'];
            $post['AndroidPayload']['displayType'] = 'notification';
            $post['AndroidPayload']['body']['title'] = $data['title'];
            $post['IosPayload']['aps']['alert']['title'] = $data['title'];
            // $post['IosPayload']['aps']['alert']['subtitle'] = $data['title'];
            $post['AndroidPayload']['body']['text'] = $data['content'];
            $post['IosPayload']['aps']['alert']['body'] = $data['content'];
            $post['IosPayload']['aps']['interruptionLevel'] = 'active';
            if (isset($this->serverInfo['logo_url']) && !empty($this->serverInfo['logo_url'])) {
                $post['AndroidPayload']['body']['img'] = $this->serverInfo['logo_url'];
            }
            if (isset($this->serverInfo['umeng_play_vibrate'])) {
                $post['AndroidPayload']['body']['playVibrate'] = ($this->serverInfo['umeng_play_vibrate']==1)?true:false;
            }
            if (isset($this->serverInfo['umeng_play_lights'])) {
                $post['AndroidPayload']['body']['playLights'] = ($this->serverInfo['umeng_play_lights'] == 1) ? true : false;
            }
            if (isset($this->serverInfo['umeng_play_sound'])) {
                $post['AndroidPayload']['body']['playSound'] = ($this->serverInfo['umeng_play_sound'] == 1) ? true : false;
            }
            $click_type = (isset($data['click_type']) && !empty($data['click_type']))?$data['click_type']: 'payload';
            switch ($click_type) {
                case 'startapp':
                    $post['AndroidPayload']['body']['afterOpen'] = 'go_app';
                    break;
                case 'url':
                    $post['AndroidPayload']['body']['afterOpen'] = 'go_url';
                    $post['AndroidPayload']['body']['url'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : [];
                    break;
                case 'payload':
                    $post['AndroidPayload']['body']['afterOpen'] = 'go_custom';
                    $post['AndroidPayload']['body']['custom'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : [];
                    $post['IosPayload']['extra'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : [];
                    break;
                default:
                    # code...
                    break;
            }
            $http = new HttpRequest;
            $sign = $this->getSign($url, $post);
            $response = $http->post($url. '?sign='.$sign, $post, 'json');
            $responseData = json_decode($response->body(), true);
            $return['send_result_data'] = $responseData;
            $return['pushParam'] = $post;
            $return['task_id'] = '';
            if (isset($responseData['Code']) && strtoupper($responseData['Code']) == '0') {
                $return['send_result'] = true;
                $return['send_error'] = '';
                $return['send_error'] = $responseData['Data']['MsgId'];
            }else{
                $return['send_error'] = '(' . $responseData['Code'] . ') ' . $responseData['Message'];
            }
            unset($post,$url,$response,$pushData,$http,$data);
            return $return;
        }
    }
    public function appPush(?array $data = null)
    {
        $return['send_result'] = false;
        if (!empty($data) && is_array($data)) {
            $url = 'https://push.openapi.umeng.com/SendByApp';
            $post['AppKey'] = $this->serverInfo['umeng_app_key'];
            $post['AndroidPayload']['displayType'] = 'notification';
            $post['AndroidPayload']['body']['title'] = $data['title'];
            $post['IosPayload']['aps']['alert']['title'] = $data['title'];
            // $post['IosPayload']['aps']['alert']['subtitle'] = $data['title'];
            $post['AndroidPayload']['body']['text'] = $data['content'];
            $post['IosPayload']['aps']['alert']['body'] = $data['content'];
            $post['IosPayload']['aps']['interruptionLevel'] = 'active';
            if (isset($this->serverInfo['logo_url']) && !empty($this->serverInfo['logo_url'])) {
                $post['AndroidPayload']['body']['img'] = $this->serverInfo['logo_url'];
            }
            if (isset($this->serverInfo['umeng_play_vibrate'])) {
                $post['AndroidPayload']['body']['playVibrate'] = ($this->serverInfo['umeng_play_vibrate']==1)?true:false;
            }
            if (isset($this->serverInfo['umeng_play_lights'])) {
                $post['AndroidPayload']['body']['playLights'] = ($this->serverInfo['umeng_play_lights'] == 1) ? true : false;
            }
            if (isset($this->serverInfo['umeng_play_sound'])) {
                $post['AndroidPayload']['body']['playSound'] = ($this->serverInfo['umeng_play_sound'] == 1) ? true : false;
            }
            $click_type = (isset($data['click_type']) && !empty($data['click_type']))?$data['click_type']: 'payload';
            switch ($click_type) {
                case 'startapp':
                    $post['AndroidPayload']['body']['afterOpen'] = 'go_app';
                    break;
                case 'url':
                    $post['AndroidPayload']['body']['afterOpen'] = 'go_url';
                    $post['AndroidPayload']['body']['url'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : [];
                    break;
                case 'payload':
                    $post['AndroidPayload']['body']['afterOpen'] = 'go_custom';
                    $post['AndroidPayload']['body']['custom'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : [];
                    $post['IosPayload']['extra'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : [];
                    break;
                default:
                    # code...
                    break;
            }
            $http = new HttpRequest;
            $sign = $this->getSign($url, $post);
            $response = $http->post($url. '?sign='.$sign, $post, 'json');
            $responseData = json_decode($response->body(), true);
            $return['send_result_data'] = $responseData;
            $return['pushParam'] = $post;
            $return['task_id'] = '';
            if (isset($responseData['Code']) && strtoupper($responseData['Code']) == '0') {
                $return['send_result'] = true;
                $return['send_error'] = '';
                $return['send_error'] = $responseData['Data']['MsgId'];
            } else {
                $return['send_error'] = '(' . $responseData['Code'] . ') ' . $responseData['Message'];
            }
            unset($post,$url,$response,$pushData,$http,$data);
            return $return;
        }
    }
    private function getSign($url,$postBody)
    {
        $postBody = is_array($postBody)?json_encode($postBody): $postBody;
        $signStr = md5("POST" . $url . $postBody . $this->serverInfo['umeng_master_secret']);
        return $signStr;
    }
}
